<?php
namespace printPictures;

class printPictures{
	
	private static $UsernameSession = "UsernameSession";
	private $comment;
	
	//types out the pictures
	//@return string (html)
	public function postPictures(){
		
		$files = glob("model/uploads/*.*");
		$colCnt=0;
		$images = "";
		for ($i=0; $i<count($files); $i++)
		    {
		    	if(stristr($files[$i], 'Thumbs.db') == FALSE){
		    			$this->comment = $this->getCommentsFromDb($files[$i]);
						$num = $files[$i];	
						$images .= '<div class="photoPosts">
					    				<a name="'.$i.'" href="'.$num.'"target="_blank"><img src="'.$num.'" width="500" height="250"/></a>
										<form name="'.$files[$i].'" class="form-inline" action="index.php?attempt" method="post" role="form">
									    	<label class="sr-only" for="Username">Username</label>
									    	<input type="text" class="form-control" name="comments" id="text" placeholder="Skriv kommentar här!">
									    	<button name="postText" type="submit" value="'.$files[$i].'" class="btn btn-default">Kommentera!</button>
								    	</form>
										<div class="comments">'. $this->comment .'</div>
									</div>';
				}
		    	
		    }
		return $images;
	}
	
	//types out the comments from database
	//@return string (html)	
	private function getCommentsFromDb($imgName){
		
		$con = mysqli_connect("10.101.1.88", "167620_mz14316", "Asd33e5fg", "167620-benny");
		// Check connection
		if (mysqli_connect_errno())
		{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
		}
		
		$result = mysqli_query($con,"SELECT * FROM comments");
		$this->comment = "";
		while($row = mysqli_fetch_array($result)){
			if($imgName == $row['Id']){
				$this->comment .= '<p>'.$row['comments']."   </br>/".$row['User'].'</p>';
				if(isset($_SESSION[self::$UsernameSession]) && $_SESSION[self::$UsernameSession] == $row['User']){
					$this->comment .= 
						" 
						<form class='form-inline' action='index.php?attempt' method='post' role='form'>
							<button id='deleteComment' name='deleteComment' value='".$row['UniqueId']."' 
							type='submit'>Delete</button>
						</form>
						<div id='commentedit".$row['UniqueId']."'>
						
						<form class='form-inline' action='index.php?attempt' method='post' role='form'>
		    				<input type='text' name='newComment' id='editText' value='".$row['comments']."' placeholder='Redigera Inlägg'>
							<button id='editComment' name='editComment' value='".$row['UniqueId']."' 
							type='submit'>Redigera</button>
						</form>
						</div>
						";
				}
			}
  		}
  		return $this->comment;
	}	
}
